import numpy as np
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# 加载波士顿房价数据集
boston = load_boston()
x = boston.data
y = boston.target

# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=666)

# 创建线性回归模型
simple2 = LinearRegression()

# 训练模型
simple2.fit(x_train, y_train)

# 打印模型系数和截距
print('多元线性回归模型系数：\n', simple2.coef_)
print('多元线性回归模型常数项：', simple2.intercept_)

# 预测测试集
y_predict = simple2.predict(x_test)

# 计算均方误差
mse = mean_squared_error(y_test, y_predict)
print('均方误差 (MSE):', mse)